
import ajax from '@/common/js/ajax.js'
import moment from 'dayjs'
/**
 * 日程
 */
let schedule = [
  {
    key: 1,
    value: '一日游'
  }, {
    key: 2,
    value: '两日游'
  }, {
    key: 3,
    value: '三日游'
  }, {
    key: 4,
    value: '四日游'
  }, {
    key: 5,
    value: '五日游'
  }, {
    key: 6,
    value: '六日游'
  }, {
    key: 7,
    value: '七日游'
  }, {
    key: 8,
    value: '八日游'
  }
]

/**
 * 标签
 */
let labelList = [
  {
    key: 1,
    value: '特色美食'
  }, {
    key: 2,
    value: '特色民宿'
  }, {
    key: 3,
    value: '自驾游'
  }, {
    key: 4,
    value: '户外体验'
  }, {
    key: 5,
    value: '亲子游'
  }, {
    key: 6,
    value: '好山好水'
  }, {
    key: 7,
    value: '农家乐'
  }, {
    key: 8,
    value: '房车体验'
  }, {
    key: 9,
    value: '露营体验'
  }
]
let tabsTitle = [
  {
    key: 1,
    value: '第一日'
  }, {
    key: 2,
    value: '第二日'
  }, {
    key: 3,
    value: '第三日'
  }, {
    key: 4,
    value: '第四日'
  }, {
    key: 5,
    value: '第五日'
  }, {
    key: 6,
    value: '第六日'
  }, {
    key: 7,
    value: '第七日'
  }, {
    key: 8,
    value: '第八日'
  }
]

let orderStatus = [
  {
    key: 10,
    value: '待付款'
  },
  {
    key: 20,
    value: '未使用'
  },
  {
    key: 90,
    value: '已完成'
  },
  {
    key: 100,
    value: '已关闭'
  },
  {
    key: 110,
    value: '待收货'
  }
  // {
  //   key: '',
  //   value: ''
  // }
]

let orderType = [
  {
    key: 1,
    value: 'ticket'
  },
  {
    key: 2,
    value: 'hotel'
  },
  {
    key: 3,
    value: 'cate'
  },
  {
    key: 4,
    value: 'specialty'
  },
  {
    key: 5,
    value: 'route'
  }
]

let dict = {
  schedule,
  labelList,
  tabsTitle,
  orderStatus,
  orderType
}

/**
 *根据code查询
 返回所有的字段
 * @param {*} code
 */
export function getListByCode (code, url, notUseSession = false) {
  return new Promise(function (resolve, reject) {
    if (!uni.getStorageSync(code)||notUseSession) {
      ajax({
        url: url || '/weChat/dictionary/queryChildrenByParentCode',
        data: {
          code: code
        }
      }).then(res=>{
		  if (!notUseSession) {
		    uni.setStorageSync(code, res.data)
		  }
		  resolve(res.data)
	  })
    } else {
      resolve(uni.getStorageSync(code))
    }
  })
}
/**
 * 简单版只返回name和code
 */
export function getLIstByCodeSimple (code) {
  return new Promise(function (resolve, reject) {
    getListByCode(code).then(data => {
      let array = data.map(t => {
        return {
          name: t.name,
          code: t.code
        }
      })
      resolve(array)
    })
  })
}

export function getValueByName (dic, key) {
  if (dict[dic]) {
    let data = dict[dic].filter((t) => {
      return t.key === key
    })
    if (data.length === 0) {
      console.error('不存在key:' + key)
    } else {
      return data[0].value
    }
  } else {
    console.error('不存在dic:' + dic)
  }
}
export function getNameByValue (dic, value) {
  if (dict[dic]) {
    let data = dict[dic].filter((t) => {
      return t.value === value
    })
    if (data.length === 0) {
      console.error('不存在value:' + value)
    } else {
      return data[0].key
    }
  } else {
    console.error('不存在dic:' + dic)
  }
}

export function getStatusList (dic) {
  if (dict[dic]) {
    return dict[dic]
  } else {
    console.error('不存在dic:' + dic)
  }
}

export function getCnTime(date){
	let monthObj={
		'January':'一月',
		'February':'二月',
		'March':'三月',
		'April':'四月',
		'May':'五月',
		'June':'六月',
		'July':'七月',
		'August':'八月',
		'September':'九月',
		'October':'十月',
		'November':'十一月',
		'December':'十二月'
	}
	let weekObj={
		'0':'周日',
		'1':'星期一',
		'2':'星期二',
		'3':'星期三',
		'4':'星期四',
		'5':'星期五',
		'6':'星期六',
	}
	return {
		cnMonth:monthObj[moment(date).format('MMMM')],
		cnWeek:weekObj[moment(date).day()],
		day:moment(date).format('DD')
	}
}